package com.daykw.xiaohongshu.task.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daykw.xiaohongshu.http.HttpBase;
import com.daykw.xiaohongshu.model.TArticleTask;
import com.daykw.xiaohongshu.model.TCommentInfo;
import com.daykw.xiaohongshu.model.TSessionId;
import com.daykw.xiaohongshu.mq.TQueue;
import com.daykw.xiaohongshu.util.CommonUtils;
import com.daykw.xiaohongshu.util.Const;
import com.daykw.xiaohongshu.util.IPPoolMap;
import com.daykw.xiaohongshu.util.MongoDBUtil;
import com.daykw.xiaohongshu.util.SessionUtils;
import com.daykw.xiaohongshu.util.SpringContextHolder;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.InsertManyOptions;

import test.XiaoHongShuSign;

public class CommentThread extends Thread {
	private static final Logger logger = Logger.getLogger(CommentThread.class);

	public CommentThread() {

	}

	@Override
	public void run() {
		consume();

	}

	public synchronized void consume() {
		NutDao xhsDao = SpringContextHolder.getBean("dao");

		SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMddHH");
		SimpleDateFormat fpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
		logger.info("****评论线程启动消息*****");

		while (true) {

			try {
				TArticleTask info = TQueue.getCQueue().take();
				TArticleTask ta = xhsDao.fetch(TArticleTask.class, Cnd.where("aid", "=", info.getAid()));
				if (ta == null || ta.getComment_status().equals(2) || ta.getComment_status().equals(5)) {

					continue;
				}
				// TAuthorTask u = xhsDao.fetch(TAuthorTask.class, Cnd.where("user_id", "=",
				// info.getUser_id()));
				// if (u == null || (u.getStatus() == 5 && StringUtils.isEmpty(u.getUrl())
				// && StringUtils.isEmpty(u.getNick()))) {
				// xhsDao.update(TArticleTask.class, Chain.make("comment_status",
				// 5).add("status", 5),
				// Cnd.where("id", "=", info.getId()));
				// logger.info("*********没有用户信息，此条评论为垃圾评论*********");
				// continue;
				// }
				int nowdate = Integer.parseInt(fdate.format(new Date()));

				// String url = "https://www.xiaohongshu.com/api/sns/v5/note/" +
				// info.getAid()
				// +
				// "/comment/list?start=5a5e14c5b42c8a64b10c34c1&num=20&show_priority_sub_comments=0&platform=Android&deviceId=2ba5c049-33de-3111-bfc3-2dd4b662d396&versionName=5.9.0&channel=Xiaomi&sid=session.1201873076495524263&lang=zh-CN&t=1517377263&sign=65d667de3065bbe68503791462f66e98";//
				// info.getUrl();
				java.util.Random r = new java.util.Random();
				String article_id = info.getAid();
				try {
					
					// GET
					// https://www.xiaohongshu.com/web_api/sns/v1/note/5b615d6c07ef1c7b45b7303f/comment?page_size=6&end_id=5b62b22109b5a30001b77c40&sid=session.1214697449282265726
					// HTTP/1.1
					// Host: www.xiaohongshu.com
					// Content-Type: application/json
					// Connection: keep-alive
					// Accept: */*
					// User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X)
					// AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A5339e MicroMessenger/6.7.1
					// NetType/WIFI Language/zh_CN
					// Referer: https://servicewechat.com/wxffc08ac7df482a27/62/page-frame.html
					// Accept-Language: zh-cn
					// Accept-Encoding: br, gzip, deflate
					// String url = "https://www.xiaohongshu.com/web_api/sns/v1/note/" + article_id
					// + "/comment?page_size=1000&sid=session.1" + r.nextInt(224697449) + "" +
					// r.nextInt(992265726);

					// String url =
					// "https://www.xiaohongshu.com/api/sns/v5/note/5b8137d6910cf63ff10ae7c1/comment/list?num=20&show_priority_sub_comments=0&platform=Android&deviceId=fa8265c4-f929-3900-90bf-ac6d321cc6ab&device_fingerprint=20180705144829b2d01289b09f5de74eb538aa504475320139e69c689426dc&device_fingerprint1=20180705144829b2d01289b09f5de74eb538aa504475320139e69c689426dc&versionName=5.23.0&channel=Store360&sid=session.1219282677194667089&lang=zh-Hans&t=1536031874";
					// https://www.xiaohongshu.com/api/sns/v5/note/5b3a2585910cf66737b05ef3/comment/list?num=20&show_priority_sub_comments=0&platform=Android&deviceId=31748f62-7b7d-33f7-84d6-9b8959e393fa&versionName=5.19.0&channel=BaiDu&sid=session.1215108254411165103&lang=zh-Hans&t=1530612642&sign=cae6113870f499b963f57302eaee1360
					String comment_url = "http://www.xiaohongshu.com/api/sns/v5/note/" + article_id + "/comment/list?";
					TSessionId sid = SessionUtils.Session.session.getQueue();
					String deviceId = sid.getDeviceId();
					String device_fingerprint = sid.getDevice_fingerprint();
					String session = sid.getSid();

					Map<String, String> headers = new HashMap<String, String>();
//					headers.put("X-Tingyun-Id","LbxHzUNcfig;c=2;r=88400402;");
//					headers.put("Authorization", "session."+session);
//					headers.put("device_id", deviceId);
//					headers.put("Accept", "*/*");
//					headers.put("User-Agent",
//							"Dalvik/1.6.0 (Linux; U; Android 4.4.2; MI 6  Build/NMF26X) Resolution/720*1280 Version/5.23.0 Build/5230179 Device/(Xiaomi ;MI 6 )");
//					headers.put("shield", "12bfe94ac559d876437d630d9851ee50");
//					headers.put("Host", "www.xiaohongshu.com");
					
					headers.put("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
					headers.put("accept-encoding","gzip, deflate, br");
					headers.put("accept-language","zh-CN,zh;q=0.9,en;q=0.8");
					headers.put("cache-control","max-age=0");
					headers.put("upgrade-insecure-requests","1");
					headers.put("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
					
					
					
					XiaoHongShuSign xhss = new XiaoHongShuSign();
					String last_comment_id = ta.getLast_aid();
					do {
						String uri = "num=20&show_priority_sub_comments=0&platform=Android&deviceId=" + deviceId
								+ "&device_fingerprint=" + device_fingerprint + "&device_fingerprint1=" + device_fingerprint
								+ "&versionName=5.23.0&channel=Store360&sid="+session
								+ "&lang=zh-Hans&t=" + (System.currentTimeMillis() / 1000);
						String url = "";
						if (StringUtils.isEmpty(last_comment_id)) {
							url = uri;
						} else {
							url = uri + "&start=" + last_comment_id;
						}
						String c_url = comment_url + url + "&sign=" + xhss.sign(url);
						// String comment_html = new HttpBase().get(url, "utf-8",
						// headers, true);
						String comment_html = new HttpBase().get(c_url, "utf-8", headers, true);
						while (StringUtils.isNotEmpty(comment_html) && comment_html.equals("fail")) {

							int c = CommonUtils.getRandomNum();
							if (c <= 4) {
								comment_html = new HttpBase().fget(c_url, "utf-8", headers, true);
							} else {
								comment_html = new HttpBase().get(c_url, "utf-8", headers, true);
							}
							if (comment_html.indexOf("Concurrent number exceeds limit") >= 0) {
								comment_html = new HttpBase().get(c_url, "utf-8", headers, true);
							}
							if (comment_html.equals("404")) {
								break;
							}

							// TimeUnit.SECONDS.sleep(3);
							// logger.info("##########请求返回Fail##########");
						}

						if (StringUtils.isNotEmpty(comment_html) && comment_html.equals("404")) {
							xhsDao.update(TArticleTask.class, Chain.make("comment_status", 4),
									Cnd.where("id", "=", info.getId()));

							continue;
						}
						// logger.info("##########请求评论返回成功##########");
						JSONObject commentjson = null;
						if (!StringUtils.isEmpty(last_comment_id)) {
							logger.info("last_comment_id:" + last_comment_id);
						}
						try {
							commentjson = JSONObject.parseObject(comment_html);
						} catch (Exception e) {
							xhsDao.update(TArticleTask.class, Chain.make("comment_status", 3),
									Cnd.where("id", "=", info.getId()));

							continue;
						}

						if (commentjson == null || commentjson.isEmpty()) {
							xhsDao.update(TArticleTask.class, Chain.make("comment_status", 3),
									Cnd.where("id", "=", info.getId()));

							continue;
						} else if (commentjson.containsKey("error_code")
								&& commentjson.getIntValue("error_code") == 30001) {
							// {"error_msg":"参数错误","data":{},"success":false,"error_code":30001}
							xhsDao.clear(TArticleTask.class, Cnd.where("id", "=", info.getId()));
							continue;
						} else {

							if (!commentjson.getBoolean("success")) {
								xhsDao.update(TArticleTask.class, Chain.make("comment_status", 3),
										Cnd.where("id", "=", info.getId()));

								continue;
							}
						}

						
						// total : 77465
						// comments_total : 36851
						JSONObject cmj = commentjson.getJSONObject("data");
						if (cmj.getInteger("comment_count") == null || cmj.getInteger("comment_count") == 0) {
							xhsDao.update(TArticleTask.class, Chain.make("comment_status", 2),
									Cnd.where("id", "=", info.getId()));
							continue;
						}
						JSONArray array = cmj.getJSONArray("comments");
						if (array == null || array.size() < 1) {
							xhsDao.update(TArticleTask.class, Chain.make("comment_status", 4),
									Cnd.where("id", "=", info.getId()));
							continue;
						}
						if (array == null || array.size() < 20) {
							last_comment_id = null;
						} else {
							last_comment_id = array.getJSONObject(array.size() - 1).getString("id");
						}
						MongoCollection<org.bson.Document> coll = MongoDBUtil.instance.getCollection(Const.MDB_DB,
								"comment");
						List<org.bson.Document> docs = new ArrayList<org.bson.Document>();
						for (int i = 0; i < array.size(); i++) {
							JSONObject c = array.getJSONObject(i);
							String comment_id = c.getString("id");
							String comment_time = c.getString("time");
							TCommentInfo cinfo = new TCommentInfo();
							try {
								cinfo.setArticle_id(article_id);
								try {
									Date pdate = new Date(Long.parseLong(comment_time) * 1000);
									cinfo.setPub_date(pdate);
									cinfo.setPdate(Integer.parseInt(date.format(pdate)));
								} catch (Exception de) {
									xhsDao.update(TArticleTask.class, Chain.make("comment_status", 3),
											Cnd.where("id", "=", info.getId()));
									continue;

								}
								cinfo.setCreate_date(nowdate);
								cinfo.setComment_id(comment_id);
								cinfo.setUrl(url);
								// cinfo.setJson(c.toJSONString());
								xhsDao.fastInsert(cinfo);
								logger.info("!!!!!!!新增一条评论" + info.getAid() + "评论队列剩余:" + TQueue.getCQueue().size()
										+ ",IP:" + IPPoolMap.getQueue().size() + "!!!!!!!");
							} catch (Exception e1) {
								// logger.error("", e1);
								// xhsDao.update(TArticleTask.class,
								// Chain.make("status", 3),
								// Cnd.where("id", "=", info.getId()));
							}

							org.bson.Document doc = org.bson.Document.parse(c.toJSONString());
							doc.put("aid", article_id);
							doc.put("cdate", nowdate);
							doc.put("pdate", cinfo.getPdate());
							docs.add(doc);

						}

						try {
							if (docs != null && !docs.isEmpty()) {
								coll.insertMany(docs, new InsertManyOptions().ordered(false));
							}

							// xhsDao.update(TArticleTask.class,
							// Chain.make("comment_status", 2),
							// Cnd.where("id", "=", info.getId()));

						} catch (Exception e) {

							// xhsDao.update(TArticleTask.class,
							// Chain.make("comment_status", 2),
							// Cnd.where("id", "=", info.getId()));

						}

						if (StringUtils.isEmpty(last_comment_id)) {
							xhsDao.update(TArticleTask.class, Chain.make("comment_status", 2),
									Cnd.where("id", "=", info.getId()));
						} else {
							xhsDao.update(TArticleTask.class, Chain.make("last_aid", last_comment_id),
									Cnd.where("id", "=", info.getId()));
						}
						// count++;
						// if (count % 2 == 0) {
						logger.info("######消费完一条评论" + info.getAid() + "评论队列剩余:" + TQueue.getCQueue().size() + ",IP:"
								+ IPPoolMap.getQueue().size());

					} while (StringUtils.isNotEmpty(last_comment_id));

				} catch (Exception e) {

					xhsDao.update(TArticleTask.class, Chain.make("comment_status", 3),
							Cnd.where("id", "=", info.getId()));

					logger.error("解析html异常:aid:" + info.getAid(), e);
				}

			} catch (Exception e) {
				logger.error("", e);

			}
		}

	}

	public static void main(String[] args) throws Exception {
		String html = new HttpBase().get("http://www.xiaohongshu.com/discovery/item/59872886d1d3b919909ace01", "utf-8",
				null, true);

		Document doc = Jsoup.parse(html);
		Elements es = doc.select("script");
		for (Element e : es) {
			String script = e.html();
			if (script.indexOf("__INITIAL_SSR_STATE__") >= 0) {
				String json = script.substring(script.indexOf("{"), script.lastIndexOf("}") + 1);
				System.out.println(json);

				JSONObject obj = JSONObject.parseObject(json);

			}
		}
		// System.out.println(es);
		// JSONArray array = parseService.parseVal(doc, requestId, url);
	}
}
